package edu.nju.mutest.util;

import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.Node;
import com.github.javaparser.ast.expr.Expression;
import com.github.javaparser.ast.visitor.VoidVisitorAdapter;
import edu.nju.mutest.visitor.collector.cond.CollectionCond;
import edu.nju.mutest.visitor.collector.cond.NoCond;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

public class CollectorUtil {

    public static List<Expression> collect(VoidVisitorAdapter<List<Expression>> collector, CompilationUnit cu) {
        return collect(collector, cu, new NoCond<>());
    }

    /**
     * collect all files within a {@link CompilationUnit} under certain conditions.
     *
     * @param collector Specify the {@link Node} types to collect.
     * @param cu the {@link CompilationUnit}
     * @param cond Declare some other collection conditions, such as node attributes and the resolved type of node.
     * @return a List of {@link Expression}
     */
    public static List<Expression> collect(
            VoidVisitorAdapter<List<Expression>> collector,
            CompilationUnit cu,
            CollectionCond<Expression> cond
    ) {
        List<Expression> exprList = new ArrayList<>();
        collector.visit(cu, exprList);
        return exprList.stream().filter(cond::willCollect).collect(Collectors.toList());
    }
}
